package fudan.se.project.repository.entity.conference.production;


import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.Conference;
import fudan.se.project.domain.conference.production.ReviewInfo;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ReviewInfoRepository extends CrudRepository<ReviewInfo, Long> {

    ReviewInfo findById(long id);

    ReviewInfo findByPcMemberUserAndPaperId(User user, long id);


    @Query("select reviewInfo from ReviewInfo reviewInfo " +
            "where reviewInfo.pcMember.user=:reviewer " +
            "and reviewInfo.id=:id ")
    ReviewInfo findByReviewerAndReviewInfoId(User reviewer, long id);

    @Query("select reviewInfo from ReviewInfo reviewInfo " +
            "where reviewInfo.paper.author.user=:author " +
            "and reviewInfo.id=:id ")
    ReviewInfo findByAuthorAndReviewInfoId(User author, long id);

    @Query("select distinct reviewInfo from ReviewInfo reviewInfo " +
            "where reviewInfo.paper.author.user=:author " +
            "and reviewInfo.paper.id=:id ")
    List<ReviewInfo> findByAuthorUserAndPaperId(User author, long id);

    @Query("select reviewInfo from ReviewInfo reviewInfo " +
            "where reviewInfo.pcMember.user=:reviewer " +
            "and reviewInfo.paper.id=:id ")
    ReviewInfo findByReviewerUserAndPaperId(User reviewer, Long id);

    int countDistinctByPaperConferenceIdAndVersion(long id, int version);

    @Query("select count(distinct reviewInfo) " +
            "from Rebuttal rebuttal " +
            "join rebuttal.post.paper.reviewInfos reviewInfo " +
            "where rebuttal.post.paper.conference.id=:id " +
            "and reviewInfo.version=:version ")
    int countDistinctByConferenceIdAnVersionInRebuttal(Long id, int version);

    List<ReviewInfo> findDistinctByPcMemberUserAndPaperConference(User user, Conference conference);

}
